from prometheus_api_client import PrometheusConnect

# Import the MetricsList and Metric modules
from prometheus_api_client import PrometheusConnect, MetricsList, Metric
import requests
import json

def node_cpu_ratio(url):
    prom = PrometheusConnect(url=url)
    my_label_config = {'instance': 'k8s-master001', 'job': 'node-exporter'}
    metric_data = prom.get_metric_range_data(metric_name='instance:node_cpu:ratio', label_config=my_label_config)
    metric_object_list = MetricsList(metric_data)
    for item in metric_object_list:
        print(item.metric_name, item.label_config, "\n")


def get_up_info():
    url = "http://172.20.216.103:13103/api/v1/query_range?query=up&start=1715864040&end=1715864100&step=15s"
    # 请求URL后将Json数据转为字典对象
    res = json.loads(requests.post(url=url).content.decode('utf8', 'ignore'))
    print(res)


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # node_cpu_ratio("https://prometheus.maka.im")
    # node_cpu_ratio("http://172.20.216.103:13103")
    # get_up_info()
    nodes = ['k8s-master001', 'k8s-node001', 'k8s-node03']
    for node in nodes:
        node_cpu_ratio("http://172.20.216.103:13103")